package com.hxz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hxz.entity.ArticleComment;
import com.hxz.entity.ReplyComment;
import com.hxz.service.ArticleCommentService;
import com.hxz.service.ReplyCommentService;
import com.hxz.util.StaticConfigParam;

import io.swagger.annotations.Api;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by Alden He on 2019/5/7
 */
@RestController
@EnableAutoConfiguration
@Api(description = "评论controller")
public class CommentRestController {
	@Autowired
	private ArticleCommentService articleCommentService;

	@Autowired
	private ReplyCommentService replyCommentService;

	/**
	 * 添加评论
	 * @param articleId
	 * @param content
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/comments/addComments",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> addComments(@RequestParam(value = "articleId", required = true) int articleId,
			@RequestParam(value = "content", required = true) String content, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> result = new HashMap<>();
		int userId = 1;
		if (articleId > StaticConfigParam.ARTICLE_ID_BEGIN && content.length() <= StaticConfigParam.COMMENT_MAX_LENGTH
				&& content != "") {
			ArticleComment articleComment = new ArticleComment(userId, articleId, content);
			result.put("status", articleCommentService.addArticleComment(articleComment) + "");
			result.put("success", "1");
		} else {
			result.put("success", "0");
		}
		return result;
	}
	/**
	 * get文章评论
	 * @param articleId
	 * @return
	 */
	@RequestMapping(value = "/comments/getComments")
	@ResponseBody
	public List<ArticleComment> getCommentsByArticleId(
			@RequestParam(value = "articleId", required = true) int articleId) {
		if (articleId >= StaticConfigParam.ARTICLE_ID_BEGIN)
			return articleCommentService.getArticleCommentsByArticleId(articleId);
		else
			return null;
	}
	/**
	 * 回复评论
	 * @param articleId
	 * @param commentId
	 * @param repliedUserId
	 * @param content
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/comments/addReplyComment")
	@ResponseBody
	public Map<String, String> addReplyComment(@RequestParam(value = "articleId", required = true) int articleId,
			@RequestParam(value = "commentId", required = true) int commentId,
			@RequestParam(value = "repliedUserId", required = true) int repliedUserId,
			@RequestParam(value = "content", required = true) String content, HttpServletRequest request) {
		int replyUserId = 0;
		Map<String, String> result = new HashMap<>();
		if (articleId >= StaticConfigParam.ARTICLE_ID_BEGIN && commentId > 0
				&& repliedUserId >= StaticConfigParam.USER_ID_BEGIN && content != ""
				&& content.length() <= StaticConfigParam.COMMENT_MAX_LENGTH) {
			ReplyComment replyComment = new ReplyComment(articleId, commentId, replyUserId, repliedUserId, content);
			int status = replyCommentService.addReplyCommentMapper(replyComment);
			result.put("status", status + "");
			result.put("success", "1");
		} else {
			result.put("success", "0");
		}
		return result;
	}
}
